package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cqvie.model.Student;

@Repository("studentDao")
public class StudentDao {
	@Resource
	protected SessionFactory sessionFactory;
	
	//学生登录
	public boolean isExistStu(int id,String pwd){
		String hql="from Student stu where stu.studentID=? and stu.stu_password=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, id);
		query.setString(1, pwd);
		if(query.list().size()>=1){
			return true;
		}else
			return false;
	}
	
	public Student  GetStudent(int stuID){
		Session session=sessionFactory.getCurrentSession();
		Student stu=(Student)session.get(Student.class, stuID);
		return stu;
	}
	
	// 根据学号查询学生
	@SuppressWarnings("unchecked")
	public List<Student> selectByStuID(int studentID) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Student stu where stu.studentID=?";
		Query query = session.createQuery(hql);
		query.setInteger(0, studentID);
		List<Student> stus=query.list();
		if (query.list().size() >= 1) {
			return stus;
		}else
			return null;
	}

	// 根据学生姓名来查询学生
	@SuppressWarnings("unchecked")
	public List<Student> selectByStuName(String name) {
		String hql = "from Student stu where stu.name like ?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, name);
		List<Student> list = query.list();
		if (query.list().size() >= 1) {
			return list;
		}else
			return null;
	}
	
	//添加学生信息
	public void addStudent(Student s){	
		Session session = sessionFactory.getCurrentSession();
		session.save(s);
	}
	
	//修改学生的信息（修改学生的班级信息）
	public boolean updatestuClass(String className,int stuID){
		int temp;
		String hql="update Student stu set stu.classes.classID=? where stu.studentID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, className);
		query.setInteger(1, stuID);
		temp=query.executeUpdate();
		if(temp>=1){
			return true;
		}else
			return false;
	}
}
